using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Simple.Contracts.Contracts;
using Simple.Contracts.Models;
using Simple.Contracts.Models.Common;
using Simple.Contracts.Models.Common.Page;
using Simple.WebAi.Authorizes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.WebAi.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("api/role")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="roleService">角色服务</param>
        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="input">角色列表输入参数</param>
        /// <returns>角色列表</returns>
        [HttpPost("list")]
        [AccountAuthorize]
        public async Task<Result<List<RoleDto>>> List(RoleListInput input)
        {
            return await _roleService.List(input);
        }

        /// <summary>
        /// 保存角色信息
        /// </summary>
        /// <param name="input">角色保存输入参数</param>
        /// <returns>保存结果</returns>
        [HttpPost("save")]
        [AccountAuthorize]
        public async Task<Result<long>> Save(RoleSaveInput input)
        {
            return await _roleService.Save(input);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="input">角色删除输入参数</param>
        /// <returns>删除结果</returns>
        [HttpPost("delete")]
        [AccountAuthorize]
        public async Task<Result<bool>> Delete(RoleDeleteInput input)
        {
            return await _roleService.Delete(input);
        }

        /// <summary>
        /// 获取所有权限树
        /// </summary>
        /// <returns>权限树</returns>
        [HttpGet("permission-tree")]
        [AccountAuthorize]
        public async Task<Result<List<PermissionDto>>> GetPermissionTree()
        {
            return await _roleService.GetPermissionTree();
        }

        /// <summary>
        /// 获取角色已分配的权限
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns>权限Id列表</returns>
        [HttpGet("role-permissions/{roleId}")]
        [AccountAuthorize]
        public async Task<Result<List<long>>> GetRolePermissions(int roleId)
        {
            return await _roleService.GetRolePermissions(roleId);
        }
    }
}